package com.aneliya.client.tcp;

import java.io.IOException;
import java.net.Socket;

import com.aneliya.client.IClient;
import com.aneliya.client.IClientFactory;
import com.aneliya.dao.IUserDAO;
import com.aneliya.io.IStreamStrategyFactory;
import com.aneliya.io.IStreamStrategy;
import com.aneliya.server.dispatcher.Dispatcher;

public class TCPClientFactory implements IClientFactory {
	private Dispatcher dispatcher;
	public void setDispatcher(Dispatcher dispatcher) {
		this.dispatcher = dispatcher;
	}
	
	private IStreamStrategyFactory streamStrategyFactory;
	public void setStreamStrategyFactory(IStreamStrategyFactory streamStrategy) {
		this.streamStrategyFactory = streamStrategy;
	}
	
	private IUserDAO userDAO;
	public void setUserDAO(IUserDAO userDAO) {
		this.userDAO = userDAO;
	}

	public IClient createClient(Object data) {
		try {
			TCPClient client = new TCPClient();
			client.setSocket((Socket) data);

			IStreamStrategy streamStrategy = streamStrategyFactory.createStrategy(
					client.getInputStream(), client.getOutputStream());

			TCPClientSender clientSender = new TCPClientSender();
			clientSender.setClient(client);
			clientSender.setDispatcher(dispatcher);
			clientSender.setStreamStrategy(streamStrategy);

			TCPClientListener clientListener = new TCPClientListener();
			clientListener.setClient(client);
			clientListener.setDispatcher(dispatcher);
			clientListener.setStreamStrategy(streamStrategy);
			clientListener.setUserDAO(userDAO);
			
			client.setClientSender(clientSender);
			client.setClientListener(clientListener);

			return client;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

}
